package com.tinshow.config.utils;

import com.tinshow.model.user.User;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.zip.GZIPInputStream;


public class WebUtils {
	

	public static final void setCurrentUser(User user, HttpServletRequest request) {
//		Subject currentUser = SecurityUtils.getSubject();
//		if (null != currentUser) {
//            HttpSession httpSession = request.getSession();
//			if (null != currentUser.getSession()) {
//                httpSession.setAttribute("current_user",user);
//                //设置session过期时间
//                httpSession.setMaxInactiveInterval(30*60);
//			}
//		}
	}

	public static final User getCurrentUser() {
//		Subject currentUser = SecurityUtils.getSubject();
//		if (null != currentUser) {
//			Session session = currentUser.getSession();
//			if (null != session) {
//				User user = (User) session.getAttribute("current_user");
//				if (null != user) {
//					return user;
//				}
//			}
//		}
		return null;
	}

    /**
     * change http post request
     * @param uri
     * @param reqHeaderMap
     * @param reqParameterMap
     * @return
     */
	public static Object doPost(String uri, Map<String, String> reqHeaderMap, Map<String,String> reqParameterMap){
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(uri);
        for(Map.Entry<String, String> entry:reqHeaderMap.entrySet()){
            postMethod.addRequestHeader(entry.getKey(),entry.getValue());
        }
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler());
        for(Map.Entry<String,String> entry: reqParameterMap.entrySet()){
            postMethod.setParameter(entry.getKey(),entry.getValue());
        }
        int status_code = 0;
        try {
            status_code = httpClient.executeMethod(postMethod);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if(status_code == HttpStatus.SC_OK){
            return postMethod;
        }
        return null;
    }

    /**
     * change http get request
     * @param uri
     * @param reqHeaderMap
     * @return
     */
    public static Object doGet(String uri, Map<String, String> reqHeaderMap){
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(uri);
        for(Map.Entry<String, String> entry:reqHeaderMap.entrySet()){
            getMethod.addRequestHeader(entry.getKey(),entry.getValue());
        }
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler());
        int status_code = 0;
        try {
            status_code = httpClient.executeMethod(getMethod);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(status_code == HttpStatus.SC_OK){
            return getMethod;
        }
        return null;
    }

    public static String getResponseBody4Zip(GetMethod get){
        StringBuffer sb = new StringBuffer();
        try {
            if(StringUtils.isNotEmpty(new String(get.getResponseBody()))){
                GZIPInputStream gzin;
                if(get.getResponseHeaders("Content-Encoding") != null
                        && get.getResponseHeader("Content-Encoding").getValue().toLowerCase().indexOf("gzip") > -1){
                    InputStream is = get.getResponseBodyAsStream();
                    gzin = new GZIPInputStream(is);
                    InputStreamReader isr = new InputStreamReader(gzin,"utf-8");
                    BufferedReader br = new BufferedReader(isr);
                    String tempbf;
                    while((tempbf = br.readLine()) != null){
                        sb.append(tempbf);
                        sb.append("\r\n");
                    }
                    isr.close();
                    gzin.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}
